package com.lanyou.esb.cook.proxy.redis.persistent.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import redis.clients.jedis.exceptions.JedisException;

import com.lanyou.esb.cook.proxy.entity.IfAvgResTimes;
import com.lanyou.esb.cook.proxy.entity.PfThroughput;
import com.lanyou.esb.cook.proxy.redis.persistent.base.BasePersistentRedis;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.PfThroughputJedisProxy;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.PfThroughputKeysJedisProxy;

/**
 * 持久化平台流量信息：将REDIS中的平台流量转存到数据中
 *
 * @author Davey.wu
 */
public class PfThroughputPersistentRedis extends
		BasePersistentRedis<PfThroughput> {
	/** Redis操作代理 **/
	private PfThroughputJedisProxy jedisProxy;
	/** 平台吞吐量键值,REDIS处理器 **/
	private PfThroughputKeysJedisProxy keysJedisProxy;

	public PfThroughputPersistentRedis() {
		// 每30秒执行一次
		this.setCron("0 0/1 * * * ?");
		this.setName("PfThroughputPersistentRedis");
		this.setGroup("PfThroughputPersistentRedis");
		this.setJobClass(this.getClass());
	}

	@Override
	public void persistent() throws Exception {
		List<PfThroughput> list = new ArrayList<>(this.getBatchAmount());
		list.clear();

		Map<String, Double> qyts = null;
		List<PfThroughput> objects = null;
		List<String> keys = null;
		List<String> qytKeys = new ArrayList<>();
		while (true) {
			try {
				qytKeys.clear();
				keys = keysJedisProxy.spopAsStr(keysJedisProxy.getKey(),
						this.getBatchReadSize());
				// 没有数据，返回
				if (keys == null || keys.isEmpty()) {
					break;
				}
				objects = jedisProxy.get(keys);
				// 没有数据，查找下一个
				if (objects == null || objects.isEmpty()) {
					continue;
				}
				for (String key : keys) {
					qytKeys.add(this.jedisProxy.getQytKey(key));
				}
				qyts = this.jedisProxy.getQty(qytKeys);
				if (qyts == null || qyts.isEmpty()) {
					continue;
				}
				this.jedisProxy.decrByFloat(qyts);
				for (PfThroughput object : objects) {
					Double value = qyts.get(this.jedisProxy
							.getQytKey(this.jedisProxy
									.getKeyFromObject(object)));
					if (value == null) {
						value = 0.0;
					}
					object.setThroughputQty(value);
				}
				list.addAll(objects);

				// 达到批量保存的数目，将数据保存到数据库
				if (list.size() >= this.getBatchAmount()) {
					getService().save(list);
					list.clear();
					continue;
				}
			} catch (Exception e) {
				if (e instanceof JedisException) {
					// e.printStackTrace();
					break;
				}
				// TODO 如果是将字符串转换成对象时出错，继续读后面的数据
				objects = null;
			}
		}
		// 保存剩余的数据
		getService().save(list);
		list.clear();
	}

	public PfThroughputJedisProxy getJedisProxy() {
		return jedisProxy;
	}

	public void setJedisProxy(PfThroughputJedisProxy jedisProxy) {
		this.jedisProxy = jedisProxy;
	}

	public PfThroughputKeysJedisProxy getKeysJedisProxy() {
		return keysJedisProxy;
	}

	public void setKeysJedisProxy(PfThroughputKeysJedisProxy keysJedisProxy) {
		this.keysJedisProxy = keysJedisProxy;
	}

}
